/*
 * ============================================================================
 *                   GNU Lesser General Public License
 * ============================================================================
 *
 *
 *
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 *
 *
 *
 */
package rad.framework.jboss;

import org.jboss.ejb3.embedded.EJB3StandaloneBootstrap;
import org.jboss.ejb3.embedded.EJB3StandaloneDeployer;

public class JBossBoostrap {

	private static EJB3StandaloneDeployer deployer;

	private static boolean started = false;

	public static boolean isStarted() {
		return started;
	}

	static public void bootstrap() {
		try {
			if (started)
				return;

			// TODO should not have to do this....
			System.setProperty(
					"javax.security.jacc.PolicyConfigurationFactory.provider",
					"org.jboss.security.jacc.JBossPolicyConfigurationFactory");

			// Boot the JBoss Microcontainer with EJB3 settings, loads
			// ejb3-interceptors-aop.xml

			// URL url =
			// Thread.currentThread().getContextClassLoader().getResource(
			// "jms-ra-4.2.jar");
			// System.out.println("RAR: " + url);

			EJB3StandaloneBootstrap.boot(null);

			// Deploy CaveatEmptor beans (datasource, mostly)
			// TODO move to deploy directory ???
			EJB3StandaloneBootstrap.deployXmlResource("jboss-jms-beans.xml");
			EJB3StandaloneBootstrap.deployXmlResource("ejb3-deployment.xml");
			EJB3StandaloneBootstrap.deployXmlResource("security-beans.xml");
			// log.debug("scanClasspath: " +
			// System.getProperty("java.class.path"));
			// EJB3StandaloneBootstrap.scanClasspath();

			deployer = EJB3StandaloneBootstrap.createDeployer();
			init(deployer);
			deployer.create();
			deployer.start();

			started = true;

		} catch (Exception ex) {
			throw new RuntimeException(ex);
		}
	}

	/**
	 * Extend or override as necessary.
	 */
	public static void init(EJB3StandaloneDeployer deployer) {
		deployer.getArchivesByResource().add("META-INF/ejb-jar.xml");
		deployer.getArchivesByResource().add(
				"org/jboss/seam/transaction/EjbSynchronizations.class");
	}

	public static void shutdown() {
		// try {
		// if (deployer != null) {
		// deployer.stop();
		// deployer.destroy();
		// }
		// EJB3StandaloneBootstrap.shutdown();
		// started = false;
		// } catch (Exception ex) {
		// throw new RuntimeException(ex);
		// }
	}

	static {
		EJB3StandaloneBootstrap.ignoredJars.add("rt.jar");
		EJB3StandaloneBootstrap.ignoredJars.add("jboss-ejb3-all-alpha8.jar");
		EJB3StandaloneBootstrap.ignoredJars.add("thirdparty-all-beta2.jar");

		EJB3StandaloneBootstrap.ignoredJars.add("hibernate-3.2.6.ga.jar");
		EJB3StandaloneBootstrap.ignoredJars.add("ejb3-persistence-1.0.1.GA.jar");
		EJB3StandaloneBootstrap.ignoredJars.add("hibernate-validator-3.0.0.GA.jar");
		EJB3StandaloneBootstrap.ignoredJars.add("hibernate-annotations-3.3.1.GA.jar");
		EJB3StandaloneBootstrap.ignoredJars.add("hibernate-commons-annotations-3.0.0.ga.jar");
		EJB3StandaloneBootstrap.ignoredJars.add("hibernate-entitymanager-3.3.2.GA.jar");

		EJB3StandaloneBootstrap.ignoredJars.add("jsf-api-1.2_09.jar");
		EJB3StandaloneBootstrap.ignoredJars.add("richfaces-api-3.2.2.GA.jar");
		EJB3StandaloneBootstrap.ignoredJars.add("jsf-facelets-1.1.14.jar");

		EJB3StandaloneBootstrap.ignoredJars.add("dumbster-1.6.jar");
		EJB3StandaloneBootstrap.ignoredJars.add("activation-1.0.2.jar");
		EJB3StandaloneBootstrap.ignoredJars.add("mail-1.4.jar");
		EJB3StandaloneBootstrap.ignoredJars.add("servlet-api-2.3.jar");
		EJB3StandaloneBootstrap.ignoredJars.add("servlet-api-2.4.jar");
		EJB3StandaloneBootstrap.ignoredJars.add("saaj-api-1.3.jar");
		EJB3StandaloneBootstrap.ignoredJars.add("jbosssha-4.0.4.GA.jar");
		EJB3StandaloneBootstrap.ignoredJars.add("jboss-client-1.0.4.jar");
		EJB3StandaloneBootstrap.ignoredJars.add("junit-4.5.jar");
		EJB3StandaloneBootstrap.ignoredJars.add("junitperf-1.8.jar");
		EJB3StandaloneBootstrap.ignoredJars.add("dbunit-2.2.jar");
		EJB3StandaloneBootstrap.ignoredJars.add("jboss-el-1.0_02.CR2.jar");
		EJB3StandaloneBootstrap.ignoredJars.add("el-api-1.0.jar");
		EJB3StandaloneBootstrap.ignoredJars.add("jboss-seam-ui-2.1.1.GA.jar");

//		EJB3StandaloneBootstrap.ignoredJars.add("antlr-2.7.5H3.jar");
//		EJB3StandaloneBootstrap.ignoredJars.add("asm-attrs.jar");
//		EJB3StandaloneBootstrap.ignoredJars.add("asm.jar");
//		EJB3StandaloneBootstrap.ignoredJars.add("cglib-2.1.1.jar");
//		EJB3StandaloneBootstrap.ignoredJars.add("commons-collections.jar");
//		EJB3StandaloneBootstrap.ignoredJars.add("commons-logging-api.jar");
//		EJB3StandaloneBootstrap.ignoredJars.add("commons-logging.jar");
//		EJB3StandaloneBootstrap.ignoredJars.add("concurrent.jar");
//		EJB3StandaloneBootstrap.ignoredJars.add("dom4j.jar");
//		EJB3StandaloneBootstrap.ignoredJars.add("hsqldb.jar");
//		EJB3StandaloneBootstrap.ignoredJars.add("javassist.jar");
//		EJB3StandaloneBootstrap.ignoredJars.add("jboss-annotations-ejb3.jar");
//		EJB3StandaloneBootstrap.ignoredJars.add("jboss-aop-jdk50.jar");
//		EJB3StandaloneBootstrap.ignoredJars.add("jboss-aspect-library-jdk50.jar");
//		EJB3StandaloneBootstrap.ignoredJars.add("jboss-common.jar");
//		EJB3StandaloneBootstrap.ignoredJars.add("jboss-common-jdbc-wrapper.jar");
//		EJB3StandaloneBootstrap.ignoredJars.add("jboss-container.jar");
//		EJB3StandaloneBootstrap.ignoredJars.add("jboss-dependency.jar");
//		EJB3StandaloneBootstrap.ignoredJars.add("jboss-ejb3.jar");
//		EJB3StandaloneBootstrap.ignoredJars.add("jboss-ejb3x.jar");
//		EJB3StandaloneBootstrap.ignoredJars.add("jboss-j2ee.jar");
//		EJB3StandaloneBootstrap.ignoredJars.add("jboss-j2se.jar");
//		EJB3StandaloneBootstrap.ignoredJars.add("jboss.jar");
//		EJB3StandaloneBootstrap.ignoredJars.add("jboss-jca.jar");
//		EJB3StandaloneBootstrap.ignoredJars.add("jboss-local-jdbc.jar");
//		EJB3StandaloneBootstrap.ignoredJars.add("jboss-microcontainer.jar");
//		EJB3StandaloneBootstrap.ignoredJars.add("jbossmq.jar");
//		EJB3StandaloneBootstrap.ignoredJars.add("jboss-remoting.jar");
//		EJB3StandaloneBootstrap.ignoredJars.add("jbosssx.jar");
//		EJB3StandaloneBootstrap.ignoredJars.add("jboss-system.jar");
//		EJB3StandaloneBootstrap.ignoredJars.add("jboss-transaction.jar");
//		EJB3StandaloneBootstrap.ignoredJars.add("jboss-xa-jdbc.jar");
//		EJB3StandaloneBootstrap.ignoredJars.add("jnpserver.jar");
//		EJB3StandaloneBootstrap.ignoredJars.add("log4j.jar");
//		EJB3StandaloneBootstrap.ignoredJars.add("resolver.jar");
//		EJB3StandaloneBootstrap.ignoredJars.add("trove.jar");
//		EJB3StandaloneBootstrap.ignoredJars.add("xercesImpl.jar");
//		EJB3StandaloneBootstrap.ignoredJars.add("xml-apis.jar");
	}

}
